
// 206.反转链表
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        // 用一个哨兵位, 进行头插
        ListNode* phead = new ListNode;
        ListNode* cur = head;
        while(cur)
        {
            ListNode* next = cur->next;
            cur->next = phead->next;
            phead->next = cur;
            cur = next;
        }
        return phead->next;
    }
};